package org.jsola.hr.service.impl;

import org.jsola.common.StrKit;
import org.jsola.core.Page;
import org.jsola.exception.ParamException;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.ITransferAccountsZfbSafetyDetailDAO;
import org.jsola.hr.dto.TransferAccountsZfbSafetyDetailAddDTO;
import org.jsola.hr.dto.TransferAccountsZfbSafetyDetailUpdateDTO;
import org.jsola.hr.entity.TransferAccountsZfbSafetyDetailDO;
import org.jsola.hr.query.TransferAccountsZfbSafetyDetailQuery;
import org.jsola.hr.service.ITransferAccountsZfbSafetyDetailService;
import org.jsola.hr.vo.TransferAccountsZfbSafetyDetailListVO;
import org.jsola.hr.vo.TransferAccountsZfbSafetyDetailVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 安全发转账明细(支付宝)
 *
 * @author zhr
 */
@Service("hrTransferAccountsZfbSafetyDetailServiceImpl")
public class TransferAccountsZfbSafetyDetailServiceImpl implements ITransferAccountsZfbSafetyDetailService {

    @Autowired
    private ITransferAccountsZfbSafetyDetailDAO transferAccountsZfbSafetyDetailDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public TransferAccountsZfbSafetyDetailVO save(TransferAccountsZfbSafetyDetailAddDTO transferAccountsZfbSafetyDetailAddDTO, TokenUser tokenUser) {
        // 转DO
        TransferAccountsZfbSafetyDetailDO transferAccountsZfbSafetyDetailDO = transferAccountsZfbSafetyDetailAddDTO.to(TransferAccountsZfbSafetyDetailDO.class);
        // 保存
        transferAccountsZfbSafetyDetailDO = save(transferAccountsZfbSafetyDetailDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return transferAccountsZfbSafetyDetailDO.to(TransferAccountsZfbSafetyDetailVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(TransferAccountsZfbSafetyDetailUpdateDTO transferAccountsZfbSafetyDetailUpdateDTO, TokenUser tokenUser) {
        //转DO
        TransferAccountsZfbSafetyDetailDO transferAccountsZfbSafetyDetailDO = transferAccountsZfbSafetyDetailUpdateDTO.to(TransferAccountsZfbSafetyDetailDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(transferAccountsZfbSafetyDetailDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... transferAccountsZfbSafetyDetailIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) transferAccountsZfbSafetyDetailIds);
    }


    @Override
    public TransferAccountsZfbSafetyDetailVO selectById(String transferAccountsZfbSafetyDetailId, String siteId) {
        TransferAccountsZfbSafetyDetailDO transferAccountsZfbSafetyDetailDO = selectDOById(transferAccountsZfbSafetyDetailId, siteId);
        if (transferAccountsZfbSafetyDetailDO == null) {
            return null;
        }
        return transferAccountsZfbSafetyDetailDO.to(TransferAccountsZfbSafetyDetailVO.class);
    }

    @Override
    public List<TransferAccountsZfbSafetyDetailListVO> select(TransferAccountsZfbSafetyDetailQuery transferAccountsZfbSafetyDetailQuery, String siteId) {
        List<TransferAccountsZfbSafetyDetailDO> transferAccountsZfbSafetyDetailDOList = selectDO(transferAccountsZfbSafetyDetailQuery, siteId);
        if(CollectionUtils.isEmpty(transferAccountsZfbSafetyDetailDOList)) {
            return transferAccountsZfbSafetyDetailDOList == null ? null : new ArrayList<>();
        }
        return transferAccountsZfbSafetyDetailDOList.stream()
                .map(transferAccountsZfbSafetyDetailDO -> transferAccountsZfbSafetyDetailDO.to(TransferAccountsZfbSafetyDetailListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(TransferAccountsZfbSafetyDetailQuery transferAccountsZfbSafetyDetailQuery, String siteId) {
        Example example = buildExample(transferAccountsZfbSafetyDetailQuery,siteId);
        return transferAccountsZfbSafetyDetailDAO.selectCountByExample(example);
    }

    @Override
    public Page<TransferAccountsZfbSafetyDetailListVO> selectPage(TransferAccountsZfbSafetyDetailQuery transferAccountsZfbSafetyDetailQuery, String siteId) {
        Example example = buildExample(transferAccountsZfbSafetyDetailQuery,siteId);
        Page<TransferAccountsZfbSafetyDetailDO> page = transferAccountsZfbSafetyDetailDAO.selectPageByExample(example,
                transferAccountsZfbSafetyDetailQuery.getPageNo(),
                transferAccountsZfbSafetyDetailQuery.getPageSize());

        return page.to(TransferAccountsZfbSafetyDetailListVO.class);
    }

    @Override
    public TransferAccountsZfbSafetyDetailDO selectDOById(String transferAccountsZfbSafetyDetailId, String siteId) {
        return listById(transferAccountsZfbSafetyDetailId, siteId);
    }

    @Override
    public TransferAccountsZfbSafetyDetailDO selectDOByIdNoSiteId(String transferAccountsZfbSafetyDetailId) {
        if (StrKit.isEmpty(transferAccountsZfbSafetyDetailId)) {
            throw new ParamException("主键id不能为空!");
        }
        Example example = new Example(TransferAccountsZfbSafetyDetailDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("id", transferAccountsZfbSafetyDetailId);
        return transferAccountsZfbSafetyDetailDAO.selectOneByExample(example);
    }

    @Override
    public List<TransferAccountsZfbSafetyDetailDO> selectDO(TransferAccountsZfbSafetyDetailQuery transferAccountsZfbSafetyDetailQuery, String siteId) {
        Example example = buildExample(transferAccountsZfbSafetyDetailQuery,siteId);
        return transferAccountsZfbSafetyDetailDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param transferAccountsZfbSafetyDetailQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(TransferAccountsZfbSafetyDetailQuery transferAccountsZfbSafetyDetailQuery, String siteId) {
        Example example = new Example(TransferAccountsZfbSafetyDetailDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (transferAccountsZfbSafetyDetailQuery.getSafetyBatchId() != null) {
            example.and().andEqualTo("safetyBatchId", transferAccountsZfbSafetyDetailQuery.getSafetyBatchId());
        }
        // 排序
        ExampleKit.setExampleOrder(example,transferAccountsZfbSafetyDetailQuery.getOrders());
        return example;
    }
}




